package cn.semyes.ylipfilter.controller.user;



import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONObject;
import cn.semyes.ylipfilter.bean.Constants;
import cn.semyes.ylipfilter.common.util.GetHttpJsonobject;
import cn.semyes.ylipfilter.common.util.R;

@RestController
@RequestMapping("/login")
public class LoginController {
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	private String params = null;
	private JSONObject res;
	private Calendar c;
	private String smsCode = null;
	private String mobileTemp = null;
	private GetHttpJsonobject getHttpJsonobject;
	
	@RequestMapping("/index.do")
	public ModelAndView getView() {
		ModelAndView mav = new ModelAndView("index.html");
		return mav;
	}
	
	@RequestMapping("/getsmscode.do")
	public void getSmsCode(HttpServletRequest request){
		res = new JSONObject();
		c = Calendar.getInstance();	
		getHttpJsonobject = new GetHttpJsonobject();
		mobileTemp = request.getParameter("mobile_num") == null ? "" : request.getParameter("mobile_num").trim();
		
		params = "mobile_num=" + mobileTemp + "&type=4&appname=原力防护";		
		res = getHttpJsonobject.httpPOST(Constants.BAIDU_APP_SMS_URL, params);
		smsCode= res.getJSONObject("attributes").getString("smscode");
			
		if (request.getSession().getAttribute("smsCode") != null || request.getSession().getAttribute("mobile_num") != null) {
			
			HttpSession httpsession = request.getSession();
			httpsession.removeAttribute("smsCode");
			httpsession.removeAttribute("mobile_num");
		}		
	
		request.getSession().setAttribute("loginTime", c.getTimeInMillis());
		request.getSession().setAttribute("smsCode", smsCode);
		request.getSession().setAttribute("mobile_num", mobileTemp);
//		logger.info("登陆"+request.getSession().getAttribute("mobile_num").toString());
		
	}
	
	@RequestMapping("/userlogin.do")
	public R login(HttpServletRequest request, HttpServletResponse response){
		String mobile = request.getParameter("mobile_num") == null ? "" : request.getParameter("mobile_num").trim();
		String inputSmsCode = request.getParameter("smsCode") == null ? "" : request.getParameter("smsCode").trim();
		
		if (request.getSession().getAttribute("smsCode") != null || request.getSession().getAttribute("mobile_num") != null) {
			smsCode = request.getSession().getAttribute("smsCode").toString();
			mobileTemp = request.getSession().getAttribute("mobile_num").toString();
			//logger.info("登录参数:"+ inputSmsCode + ":" + smsCode + "  : "+mobile+ " : " + mobileTemp);
					
			if (mobile.equals(mobileTemp) && smsCode.equals(inputSmsCode)) {
				request.getSession().setAttribute("userAuthorization", mobileTemp+smsCode);	
				return R.ok().put("data", "http://bdtest.ipfilter.nisure.net/home/index.do");
//				return R.ok().put("data", "http://bdapp.ylipfilter.nisure.cn/home/index.do");
			}else {
				return R.ok().put("data", "手机或验证码输入错误！");
			}
			
		}		
		
		return R.ok().put("data", "手机或验证码输入错误！");
		
	}

}
